﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using Point = System.Drawing.Point;


namespace DragAndDrop
{
    class MoveControlHelper
    {
        public enum MoveDirection
        {
            Any,
            X,
            Y
        }

        public static void Initiallize(Control control)
        {
            Initiallize(control, MoveDirection.Any);
        }

        public static void Initiallize(Control control, MoveDirection direction)
        {
            Initiallize(control, control, direction);
        }

        public static void Initiallize(Control control, Control container, MoveDirection direction)
        {
            var dragging = false;
            Point dragStart = Point.Empty;
            control.MouseDown += delegate(object sender, MouseEventArgs e)
            {
                dragging = true;
                dragStart = new Point(e.X, e.Y);
                control.Capture = true;
            };
            control.MouseUp += delegate
            {
                dragging = false;
                control.Capture = false;
            };
            control.MouseMove += delegate(object sender, MouseEventArgs e)
            {
                if (!dragging) return;
                if (direction != MoveDirection.Y)
                    container.Left = Math.Max(0, e.X + container.Left - dragStart.X);
                if (direction != MoveDirection.X)
                    container.Top = Math.Max(0, e.Y + container.Top - dragStart.Y);
                container.BringToFront();
            };
        }
    }
}
